param(
	#[string]$receiptRootSiteUrl="$(Read-Host 'Enter the receipt Root web url. [e.g. http://sites/receipts]')",
	[string]$receiptYear = "$(Read-Host 'Enter the Year for the receipt. [e.g. 2012]')",
	[string]$forceDeleteExistingSite = $TRUE
)


function Update-Scopes($siteUrl)
{
	[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint") | out-null
	[void][reflection.assembly]::Loadwithpartialname("Microsoft.office.server.search") | out-null
	
	$s = [microsoft.sharepoint.spsite]$siteUrl
	$sc = [microsoft.office.server.servercontext]::GetContext($s)
	$search = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($sc)
	$scopes = [microsoft.office.server.search.administration.scopes]$search
	$scopes.StartCompilation()
	while ($scopes.CompilationPercentComplete -lt 100) { sleep -seconds 3; write-host "$($scopes.CompilationPercentComplete)% complete" }
}


#Load variables from configuration file
[System.Xml.XmlDocument] $xmlDoc = New-Object System.Xml.XmlDocument
$file = Resolve-Path (".\deployment.config")

if(!$file){
	Write-Host "Could not find the configuration file deployment.cnofig. Aborting." -ForegroundColor Red
	break
}

Write-Host "Parsing file: " $file
$xmlDoc = [xml](Get-Content $file)
$siteCollectionUrl = $xmlDoc.Configuration.WebApplication.SiteCollection.Url
$receiptRootSiteUrl = $xmlDoc.Configuration.WebApplication.SiteCollection.ReceiptSite.Url
$searchService = $xmlDoc.Configuration.SearchService.Name
$scopeName =$xmlDoc.Configuration.SearchService.ReceiptSearchScope.Name


if(!$receiptRootSiteUrl){
	Write-Host "Can't Load Receipt Root Site Url. Aborting." -ForegroundColor Red
	break
}


$receiptWebTemplateID = "STS#0" #team site
$LOCALE_ID_ENGLISH = 1033
$receiptListName = "Receipts"
$receiptListTitle = "Receipts"
$receiptDocumentName = "ReceiptDocuments" 
$receiptDocumentTitle = "Receipt Documents" 
$documentTemplateId = 101
$receiptListTemplateName = "Receipt Definition"

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue


$receiptWeb = Get-SPWeb -identity $receiptRootSiteUrl

$siteExist = $receiptWeb.Webs[$receiptYear].Exists;

if(($siteExist) -and ($forceDeleteExistingSite -eq $FALSE)){
	$currentSiteUrl = $receiptWeb.Webs["$receiptYear"].Url
	Write-Host "This site $currentSiteUrl already exist" -ForegroundColor Red
}
else{	
	if(!$receiptRootSiteUrl.EndsWith("/")){
		$receiptRootSiteUrl = $receiptRootSiteUrl + "/"
	}
	$webUrl = $receiptRootSiteUrl + $receiptYear
	if($siteExist){
		#delete the site
		Remove-SPWeb -identity $webUrl -Confirm:$false
		Write-Host "Removing site $webUrl..."
		Start-Sleep -Seconds 30
	}
	#good to go, create the site
	#$siteTemplate = $receiptWeb.GetAvailableWebTemplates($LOCALE_ID_ENGLISH)["$receiptWebTemplateID"];
	#$receiptWeb.Webs.Add("$receiptYear","$receiptYear","", $LOCALE_ID_ENGLISH,$siteTemplate,"false","false")

	New-SPWeb -Url $webUrl -Template  $receiptWebTemplateID -Name $receiptYear
	Write-Host "Creating new receipt site $webUrl..."
	Start-Sleep -Seconds 30
	
	$currentReceiptWeb = Get-SPWeb -identity $webUrl
	
	#create receipt document library
	$receiptDocumentGuid = $currentReceiptWeb.Lists.Add($receiptDocumentName, $receiptDocumentTitle, 101)
	$receiptDocument = $currentReceiptWeb.Lists[$receiptDocumentGuid]
	if($receiptDocument){
		Write-Host "Receipt Document Library created successfully." -ForegroundColor Green
	}

	$listTemplate = $receiptWeb.ListTemplates[$receiptListTemplateName]; 

	#create receipt list
	$receiptListGuid = 	$currentReceiptWeb.Lists.Add($receiptListName, $receiptListTitle, $listTemplate)
	$receiptList =  $currentReceiptWeb.Lists[$receiptListGuid]
	if($receiptList){
		Write-Host "Receipt List created successfully." -ForegroundColor Green
		$receiptList.EnableFolderCreation=1
		$receiptList.Update();
	}
	$crawlListUrl = "$webUrl/Lists/$receiptListName"
	$currentReceiptWeb.Dispose()
	
	#add crawl rule to the search scope 
	$ssa = Get-SPEnterpriseSearchServiceApplication -Identity $searchService
	$scope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $ssa
	$sr = New-SPEnterpriseSearchQueryScopeRule -Scope $scope -RuleType Url -FilterBehavior Include -UrlScopeRuleType Folder -MatchingString $crawlListUrl -Url $receiptRootSiteUrl
	if($sr -ne $null){
		Write-Host "Search Scope Rule for $crawlListUrl is successfully created." -ForegroundColor Green	
	}else{
		Write-Host "Search Scope Rule failed to Created. Manually add search scope rule is required!" -ForegroundColor Red
	}
	
	
}

$receiptWeb.Dispose()

Update-Scopes -siteUrl $siteCollectionUrl

#add scope rule to this receipt list to enable searching


Remove-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue


